const express = require('express')
const c = require('../connection')
const router = express.Router()
//查询员工姓名
router.get('/memberList', (req, res) => {
    c.query(`select name from uc_member`, (err, r) => {
        if (err) {
            throw err
        }
        if (r.length > 0) {
            res.send({
                code: 200,
                msg: '查询成功',
                data: r
            })
        } else {
            res.send({
                code: 401,
                msg: '查询失败',
            })
        }
    })
})
//查询用户头像
router.get('/pic', (req, res) => {
    var obj = req.query
    c.query(`select pic from uc_member where name=?`, [obj.name], (err, r) => {
        if (err) {
            throw err
        }
        if (r.length > 0) {
            res.send({
                code: 200,
                msg: '查询成功',
                data: r
            })
        } else {
            req.send({
                code: 401,
                msg: '查询失败'
            })
        }
    })
})
//权限列表
router.get('/level', (req, res) => {
    c.query(`select * from uc_system`, (err, r) => {
        if (err) {
            throw err
        }
        if (r.length > 0) {
            res.send({
                code: 200,
                msg: '列表查询成功',
                data: r
            })
        } else {
            res.send({
                code: 401,
                msg: '查询失败'
            })
        }
    })
})
//修改用户信息
router.post('/update', (req, res) => {
    var obj = req.body
    c.query(`update uc_user set ? where username=?`, [obj, obj.username], (err, r) => {
        if (err) {
            throw err
        }
        if (r.changedRows === 0) {
            res.send({
                code: 401,
                msg: '修改失败'
            })
        } else {
            res.send({
                code: 200,
                msg: '修改成功',
                data: r
            })
        }
    })
})
module.exports = router
